package com.zhipu.balance.ui.vm.home.calculate.dami

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordDaMi
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 大米碎米
 */
class DaMiBrokenViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 大米实体
     */
    var mRecordDaMi : RecordDaMi? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 碎米-试样质量
     */
    var qualityOfBrokenRiceSample = ObservableField("")


    /**
     * 碎米质量
     */
    var brokenRiceQuality =  ObservableField("")

    /**
     * 碎米率
     */
    var totalBrokenRice =  ObservableField("")

    /**
     * 小碎米质量
     */
    var smallBrokenRiceQuality =  ObservableField("")

    /**
     * 小碎米率
     */
    var smallBrokenRice =  ObservableField("")




    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordDaMi?.brokenRiceRecorder)) {
            //碎米-试样质量
            qualityOfBrokenRiceSample.set(mRecordDaMi?.qualityOfBrokenRiceSample.toString())
            //碎米质量
            brokenRiceQuality.set(mRecordDaMi?.brokenRiceQuality.toString())
            //碎米率
            totalBrokenRice.set(mRecordDaMi?.totalBrokenRice.toString())
            //小碎米质量
            smallBrokenRiceQuality.set(mRecordDaMi?.smallBrokenRiceQuality.toString())
            //小碎米率
            smallBrokenRice.set(mRecordDaMi?.smallBrokenRice.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //碎米-试样质量
        qualityOfBrokenRiceSample.set("")
        //碎米质量
        brokenRiceQuality.set("")
        //碎米率
        totalBrokenRice.set("")
        //小碎米质量
        smallBrokenRiceQuality.set("")
        //小碎米率
        smallBrokenRice.set("")
    }




    /**
     * 试样质量
     */
    fun BulkQuality(){
        qualityOfBrokenRiceSample.set(weighing)
        mRecordDaMi?.qualityOfBrokenRiceSample = qualityOfBrokenRiceSample.get()?.toDouble()
    }

    /**
     * 碎米质量
     */
    fun MassOfBrokenRice(){
        brokenRiceQuality.set(weighing)
        mRecordDaMi?.brokenRiceQuality = brokenRiceQuality.get()?.toDouble()
    }

    /**
     * 小碎米质量
     */
    fun SmallBrokenRiceMass(){
        smallBrokenRiceQuality.set(weighing)
        mRecordDaMi?.smallBrokenRiceQuality = smallBrokenRiceQuality.get()?.toDouble()
    }

    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算碎米率
         */
        var mTotalBrokenRice : Double = CommonUtils.formatDouble(mRecordDaMi!!.brokenRiceQuality / mRecordDaMi!!.qualityOfBrokenRiceSample * 100)
        mRecordDaMi?.totalBrokenRice = mTotalBrokenRice
        totalBrokenRice.set(mTotalBrokenRice.toString())
        /**
         *  小碎米率
         */
        var mSmallBrokenRice: Double = CommonUtils.formatDouble(mRecordDaMi!!.smallBrokenRiceQuality / mRecordDaMi!!.qualityOfBrokenRiceSample * 100)
        mRecordDaMi?.smallBrokenRice = mSmallBrokenRice
        smallBrokenRice.set(mSmallBrokenRice.toString())

        /**
         * 小碎米记录员
         */
        mRecordDaMi!!.brokenRiceRecorder = mPersonnel!!.name


        //碎米-试样质量
        if(mRecordDaMi?.qualityOfBrokenRiceSample == 0.00){
            qualityOfBrokenRiceSample.set("0.00")
        }
        //碎米质量
        if(mRecordDaMi?.brokenRiceQuality == 0.00){
            brokenRiceQuality.set("0.00")
        }
        //小碎米质量
        if(mRecordDaMi?.smallBrokenRiceQuality == 0.00){
            smallBrokenRiceQuality.set("0.00")
        }
    }
}